package com.kedacom.ctsp.authority.util;

import com.kedacom.ctsp.lang.ClassUtil;
import com.kedacom.ctsp.lang.StringUtil;
import com.kedacom.ctsp.lang.mapper.factory.MapperEntityFactory;
import com.kedacom.ctsp.web.controller.util.ServletUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @com.kedacom.stsp.authority.util
 * @fenghaiju
 * @2018/4/20-13:02
 * @cloud-rbac
 **/
public class QueryParamShowUtil {

    public static final String packageName = "com.kedacom.ctsp.authority.vo";//实例包名
    public static final String exclude = "exclude";//排除
    public static final String include = "include";//包含

    public static Map paddingFields(HttpServletRequest request) {
        return paddingFields(request, "m");

    }

    public static Map paddingFields(HttpServletRequest request, String prefix) {
        Map maps = buildFrom(request, prefix);
        return maps;
    }

    /**
     * 从HttpRequest中创建不要显示字段列表和显示字段列表
     * Term命名规则为prefix属性前缀_比较类型_属性名.
     * <p>
     * eg.
     * f_eq_name
     * f_like_name_or_email
     */
    private static Map buildFrom(HttpServletRequest request, String prefix) {
        Map map = new HashMap();

        //从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
        Map<String, Object> filterParamMap = ServletUtil.getParametersStartingWith(request, prefix + "_");

        //分析参数Map,构造PropertyFilter列表
        for (Map.Entry<String, Object> entry : filterParamMap.entrySet()) {
            String filterName = StringUtils.substringBefore(entry.getKey(), "-");
            Object value = entry.getValue();
            //如果value值为空,则忽略此filter.
            if (value == null) {
                continue;
            }
            String strValue = StringUtil.toString(value);
            if (StringUtils.isBlank(strValue)) {
                continue;
            }
            Map filter = buildTerm(filterName, strValue);
            map.put(filterName, filter);


        }

        return map;
    }


    /**
     * @param name  比较属性字符串,含待比较的比较类型、属性值类型及属性列表.
     *              eg. like_name_or_login_name
     * @param value 待比较的值.
     */
    private static Map<Object, String> buildTerm(String name, String value) {
        if (StringUtils.isBlank(name)) {
            return null;
        }
        if (StringUtils.isBlank(value)) {
            return null;
        }
        String[] values = StringUtils.splitByWholeSeparator(value, ".");
        if (values.length != 2) {
            return null;
        }
        Map<Object, String> map = new HashMap<>();
        String className = values[0];
        try {
            Class c = Class.forName(packageName + "." + className);
            if (ClassUtil.isInterfaceOrAbstractClass(c)) {
                c = MapperEntityFactory.get().getInstanceType(c);
            }
            if (ClassUtil.isInterfaceOrAbstractClass(c)) {
                return null;
            }

            try {
                map.put(c.newInstance(), values[1]);
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

        return map;

    }
}
